import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;


public class RobotWindow extends JFrame implements ActionListener {

	private static final long serialVersionUID = 2335039543484597626L;
	
	private static final int windowWidth = 200;
	private static final int windowHeight = 510;
	private static final int window_xLocation = 760;
	private static final int window_yLocation = 280;
	private static final int imageLabelWidth = 188;
	private static final int imageLabelHeight = 200;
	
    private JButton captureButton;
    private JButton colorFilterButton;
    private JButton detectBoardButton;
    private JButton startSolvingButton;
    private JButton loadImageButton;
    private JButton saveImageButton;
    private JButton undoButton;
    private JPanel mainPanel;
    private JLabel redColorLabel;
    private JLabel greenColorLabel;
    private JLabel blueColorLabel;
    private JLabel imageLabel;
    private JTextField redColorField;
    private JTextField greenColorField;
    private JTextField blueColorField;
    private JLabel robotControlField;
	
	public RobotWindow() {
		super();
		
		//Positioning
		setSize(windowWidth, windowHeight);
		setLocation(window_xLocation, window_yLocation);
		//Window properties
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setAlwaysOnTop(true);
		setResizable(false);
		
		InitComponents();
	}
	
	private void InitComponents() {
        mainPanel = new javax.swing.JPanel();
        robotControlField = new javax.swing.JLabel();
        captureButton = new javax.swing.JButton();
        detectBoardButton = new javax.swing.JButton();
        startSolvingButton = new javax.swing.JButton();
        colorFilterButton = new javax.swing.JButton();
        loadImageButton = new javax.swing.JButton();
        saveImageButton = new javax.swing.JButton();
        undoButton = new javax.swing.JButton();
        redColorLabel = new javax.swing.JLabel();
        greenColorLabel = new javax.swing.JLabel();
        blueColorLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();
        redColorField = new javax.swing.JTextField();
        greenColorField = new javax.swing.JTextField();
        blueColorField = new javax.swing.JTextField();

        mainPanel.setPreferredSize(new java.awt.Dimension(windowWidth, windowHeight));

        robotControlField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        robotControlField.setText("Free");
        robotControlField.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        robotControlField.setBackground(Color.green);

        captureButton.setText("Capture Screen");
        captureButton.setActionCommand("capture");
        captureButton.addActionListener(this);

        detectBoardButton.setText("Detect Game Frame");
        detectBoardButton.setActionCommand("detectBoard");
        detectBoardButton.addActionListener(this);
        
        startSolvingButton.setText("Start Solving");
        startSolvingButton.setActionCommand("startSolving");
        startSolvingButton.addActionListener(this);

        colorFilterButton.setText("Color Filter");
        colorFilterButton.setActionCommand("colorfilter");
        colorFilterButton.addActionListener(this);
        
        loadImageButton.setText("Load");
        loadImageButton.setActionCommand("load");
        loadImageButton.addActionListener(this);
        
        saveImageButton.setText("Save");
        saveImageButton.setActionCommand("save");
        saveImageButton.addActionListener(this);
        
        undoButton.setText("Undo");
        undoButton.setActionCommand("undo");
        undoButton.addActionListener(this);
        
        redColorLabel.setText("R:");
        greenColorLabel.setText("G:");
        blueColorLabel.setText("B:");
        
        imageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        imageLabel.setBackground(Color.black);
        imageLabel.setOpaque(true);
        imageLabel.setPreferredSize(new Dimension(imageLabelWidth ,imageLabelHeight));

        redColorField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        redColorField.setText("0");

        greenColorField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        greenColorField.setText("0");

        blueColorField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        blueColorField.setText("0");

        GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(Alignment.LEADING)
            .addComponent(robotControlField, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel, GroupLayout.DEFAULT_SIZE, imageLabelWidth, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(detectBoardButton, GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(startSolvingButton, GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(captureButton, GroupLayout.PREFERRED_SIZE, 166, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loadImageButton, GroupLayout.PREFERRED_SIZE, 80, Short.MAX_VALUE)
                .addComponent(saveImageButton, GroupLayout.PREFERRED_SIZE, 80, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(colorFilterButton, GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(undoButton, GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(redColorLabel)
                .addComponent(redColorField)
                .addComponent(greenColorLabel)
                .addComponent(greenColorField)
                .addComponent(blueColorLabel)
                .addComponent(blueColorField)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(captureButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(Alignment.BASELINE)
                		.addComponent(loadImageButton)
                		.addComponent(saveImageButton))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(detectBoardButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(startSolvingButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(Alignment.BASELINE)
                	.addComponent(redColorLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(redColorField, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(greenColorLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(greenColorField, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(blueColorLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(blueColorField, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(colorFilterButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(undoButton, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                .addComponent(robotControlField, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(imageLabel, GroupLayout.PREFERRED_SIZE, imageLabelHeight, GroupLayout.PREFERRED_SIZE))
        );

        add(mainPanel);
    }

	public void SetImage(Image image) {
		float imageWidth = image.getWidth(null);
		float imageHeight = image.getHeight(null);
		int scaledWidth = imageLabelWidth;
		int scaledHeight = imageLabelHeight;
		if(imageWidth / imageLabelWidth > imageHeight / imageLabelHeight) {
			scaledHeight = (int) (imageHeight * (scaledWidth/imageWidth));
		} else {
			scaledWidth = (int) (imageWidth * (scaledHeight/imageHeight));
		}
		AreaAveragingScaleFilter scaleFilter = new AreaAveragingScaleFilter(scaledWidth, scaledHeight);
		Image scaledimage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), scaleFilter));
		imageLabel.setIcon(new ImageIcon(scaledimage));
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("capture")) {
			Application.GetImageManager().CaptureScreen();
		} else if (e.getActionCommand().equals("colorfilter")) {
			Application.GetImageManager().RunColorFilter(Integer.parseInt(redColorField.getText()), Integer.parseInt(greenColorField.getText()), Integer.parseInt(blueColorField.getText()));
		} else if (e.getActionCommand().equals("detectBoard")) {
			Application.GetImageManager().DetectGameFrame();
		} else if (e.getActionCommand().equals("startSolving")) {
			if(!Application.IsSolving()) {
				Application.StartSolving();
				startSolvingButton.setText("Move mouse to stop");
			}
		} else if (e.getActionCommand().equals("load")) {
			LoadImage();
		} else if (e.getActionCommand().equals("save")) {
			Application.GetImageManager().SaveImage();
		} else if (e.getActionCommand().equals("undo")) {
			Application.GetImageManager().Undo();
		}
	}
	
	private void LoadImage() {
		JFileChooser chooser = new JFileChooser();
		ImageFileFilter filter = new ImageFileFilter();
		filter.addExtension("jpg");
		filter.addExtension("gif");
		filter.addExtension("png");
		filter.setDescription("JPG, GIF, PNG Images");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(this);
		if(returnVal == JFileChooser.CANCEL_OPTION)
			return;
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			Application.GetImageManager().SetImage(chooser.getSelectedFile());
			SetImage(Application.GetImageManager().currentImage);
		}
	}
	
	public void ResetSolvingButton() {
		startSolvingButton.setText("Start Solving");
	}
}
